if (!chinahometex) {
	var chinahometex = {};
};

if (!chinahometex.util) {
	chinahometex.util = {};
}

if (!chinahometex.ui) {
	chinahometex.ui = {};
};

chinahometex.util.$ = function(id) {
	return document.getElementById(id);
};
chinahometex.util.StringBuffer = function(s) {
	Object.call(this);
	if (s && typeof (s) == 'string') {
		this.charsequence = s;
	}
};
chinahometex.util.StringBuffer.prototype = new Object();
chinahometex.util.StringBuffer.prototype.charsequence = null;
chinahometex.util.StringBuffer.prototype.append = function(s) {
	this.charsequence += s;
	return this;
};
chinahometex.util.StringBuffer.prototype.substring = function(s, e) {
	return new chinahometex.util.StringBuffer(this.toString().substring(s, e));
};

chinahometex.util.StringBuffer.prototype.toString = function() {
	return this.charsequence;
};

chinahometex.ui.Panel = function(container, model, f) {
	Object.call(this);
	this.container = container;
	this.model = model;
	if (!f) {
		this.container.innerHTML = this.paint();
	}
};

chinahometex.ui.Panel.prototype = new Object();

chinahometex.ui.Panel.prototype.container = null;
chinahometex.ui.Panel.prototype.model = null;
chinahometex.ui.Panel.prototype.paint = function() {
	return null;
};

/**
 * model : { defaultImage : '', }
 */
chinahometex.ui.UploadDialog = function(container, model, f) {
	chinahometex.ui.Panel.call(this, container, model, f);
};

chinahometex.ui.UploadDialog.prototype = new chinahometex.ui.Panel({});
chinahometex.ui.UploadDialog.prototype.paint = function() {
	var buffer = new chinahometex.util.StringBuffer("<div id='");
	buffer.append(this.model.id).append("'>");
	buffer
			.append("<img src='http://img.baidu.com/img/image/ilogos.gif' style='display:none' onload=\"var url='");
	buffer.append(this.model.defaultImage).append(
			"'; var img = chinahometex.util.$('");
	buffer.append(this.model.id)
			.append("_img'); handle_change(url,img); \" />");

	buffer.append("").append("<span id='").append(this.model.id).append(
			"_img' ");
	buffer
			.append("style='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);border-style:solid; border-width:1px;'></span><br/>");
	buffer.append("<input type='file' name='").append(this.model.property)
			.append("' ");
	buffer.append(" onchange=\"var fd = document.getElementById('").append(
			this.model.id).append("_img").append(
			"'); handle_change(this.value,fd);\" >");
	buffer.append("</div>");

	return buffer.toString();
};

var handle_change = function(url, img) {
	img.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = url;
	img.style.width = "100px";
	img.style.height = "100px";
}

chinahometex.ui.Functions = {
	/**
	 * ptr: {target: object,parameters : [{ name : string, value : string }],
	 * action : string }
	 */
	onCasecadeComboBoxChanging : function(ptr) {
		var httpclient = new chinahometex.cm.HttpClient();
		for ( var i = 0; i < ptr.parameters.length; i++) {
			httpclient.setParameter(ptr.parameters[i].name,
					ptr.parameters[i].value);
		}
		httpclient.action = ptr.action;

		var tgt = ptr.source.form[ptr.target];

		httpclient
				.doPost(
						AJAX_SERVLET,
						function(p) {

							if (p.readyState == 4) {
								if (p.status == 200) {
									var shellcode = "var sc = "
											+ p.responseText;

									eval(shellcode);

									if (sc.code != 200) {
										alert("Internal System Error, please contact adminitrator!");
										return;
									}

									tgt.innerHTML = "";
									tgt.options.add(new Option("All",""));
									for ( var i = 0; i < sc.data.length; i++) {
										var option = new Option(
												sc.data[i].sname,
												sc.data[i].nid);
										tgt.options.add(option);
									}
								}
							}
						});
	}
}
